package com.example.mallsystem1104.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mallsystem1104.constants.Constants;
import com.example.mallsystem1104.constants.RedisConstants;
import com.example.mallsystem1104.entity.LoginForm;
import com.example.mallsystem1104.entity.User;
import com.example.mallsystem1104.entity.dto.UserDTO;
import com.example.mallsystem1104.exception.ServiceException;
import com.example.mallsystem1104.mapper.UserMapper;
import com.example.mallsystem1104.utils.TokenUtils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author zy
 * @Date 2024/12/12 9:54
 * @packageName:com.example.mallsystem1104.service
 * @ClassName:UserService
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String,User> redisTemplate;

    public UserDTO login(LoginForm loginForm) {
        //1.查询用户是否存在
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userName",loginForm.getUsername());
        queryWrapper.eq("password",loginForm.getPassword());
        User user=getOne(queryWrapper);

        //2.用户不存在
        if(user==null){
            throw new ServiceException(Constants.CODE_403,"用户名和密码错误");
        }
        String token= TokenUtils.getToken(user.getId().toString(),user.getUsername());
        //把用户存到redis中
        redisTemplate.opsForValue().set(RedisConstants.USER_TOKEN_KEY+token,user);
        //设置redis的过期时间
        redisTemplate.expire(RedisConstants.USER_TOKEN_KEY+token,RedisConstants.USER_TOKEN_TTL, TimeUnit.MINUTES);
        //把获取到user赋值到userDTO中
        UserDTO userDTO= BeanUtil.copyProperties(user,UserDTO.class);
        //设置token
        userDTO.setToken(token);

        return userDTO;
    }

    public User register(LoginForm loginForm) {
        //1.查询用户是否存在
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userName",loginForm.getUsername());
        User user=getOne(queryWrapper);

        //2.用户存在
        if(user!=null){
            throw new ServiceException(Constants.CODE_403,"用户已注册");
        }else {
            user=new User();
            BeanUtil.copyProperties(loginForm,user);
            user.setNickname("新用户");
            user.setRole("user");
            save(user);
            return user;
        }
    }

    public User getOneUser(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return getOne(queryWrapper);

    }

    public void resetPassword(String id, String newPassword) {
        User user=this.getById(id);
        if (user==null) return;
        user.setPassword(newPassword);
        this.updateById(user);

    }
}
